<?php
namespace App\Service\Member;

class PatientService extends BaseService
{
    //患者

    /**
     * 列表
     */
    public function getList($uid)
    {
        $db = $this->dbConn($this->dbPatient)
            ->where('del', 0)
            ->where('uid', $uid);
        $total = $db->count();
        $res = $db->skip(0)->take(10)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbPatient)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $imgList = [];
        $imgOnes = $this->dbConn($this->dbPatImg)
            ->where('del', 0)
            ->where('up_id', $id)
            ->get();
        if (count($imgOnes)) {
            foreach ($imgOnes as $v) {
                if (file_exists($v->img)) {
                    $imgList[] = '/'.$v->img;
                }
            }
        }
        $arys[] = [
            'id' => $res['id'],
            'title' => $res['title'],
            'detail' => $res['detail'],
            'realname' => $res['realname'],
            'age' => $res['age'],
            'sex_name' => $res['sex']==1 ? '男' : '女',
            'imgList' => $imgList,
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param array $pas
     * @param array $imgs
     */
    public function add($pas, $imgs)
    {
        $res = (array) $this->dbConn($this->dbPatient)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->where('uid', $pas['uid'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在该病历');
        }
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $pas['uid'])
            ->first();
        if (empty($uOne)) {
            return $this->toback(-2, '没有登录');
        }
        if (!$pas['realname']) {
            $pas['realname'] = $uOne['realname'];
        }
        if (!$pas['sex']) {
            $pas['sex'] = $uOne['sex'];
        }
        if (!$pas['age']) {
            $pas['age'] = $uOne['age'];
        }
        $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbPatient)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        foreach ($imgs as $v) {
            $fpath = substr($v, 0, 1)=='/' ? substr($v, 1) : '';
            if (file_exists($fpath)) {
                $this->dbConn($this->dbPatImg)->insertGetId([
                    'up_id' => $lastid, 'img' => $fpath, 'addtime' => toDate(),
                ]);
            }
        }
        $resData = [
            'id' => $lastid,
            'title'=>$pas['title']
        ];
        return $this->toback(0, '', $resData);
    }

    /**
     * 修改
     * @param int $id
     * @param array $pas
     * @param array $imgs
     */
    public function modify($id, $pas, $imgs)
    {
        $res = (array) $this->dbConn($this->dbPatient)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有病历');
        }
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $pas['uid'])
            ->first();
        if (empty($uOne)) {
            return $this->toback(-2, '没有登录');
        }
        if (!$pas['realname']) {
            $pas['realname'] = $uOne['realname'];
        }
        if (!$pas['sex']) {
            $pas['sex'] = $uOne['sex'];
        }
        if (!$pas['age']) {
            $pas['age'] = $uOne['age'];
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbPatient)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        $links = [];
        foreach ($imgs as $v) {
            $imgOne = (array) $this->dbConn($this->dbPatImg)
                ->where('del', 0)
                ->where('up_id', $id)
                ->where('img', $v)
                ->first();
            if (empty($imgOne)) {
                $this->dbConn($this->dbPatImg)->insertGetId([
                    'up_id' => $id, 'img' => $v, 'addtime' => toDate(),
                ]);
            }
            $links[] = $v;
        }
        $this->dbConn($this->dbPatImg)
            ->where('del', 0)
            ->where('up_id', $id)
            ->whereNotIn('img', $links)
            ->update(['del' => 1, 'deltime' => toDate()]);
        return $this->toback();
    }
}